package sv.gob.mh.dgii.model.validators;

import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import sv.gob.mh.dgii.model.custom.constraints.Nit;


public class NitValidator implements ConstraintValidator<Nit, String> {

     
    
	 	private boolean conGuiones;

	    public void initialize(Nit constraintAnnotation) {
	        this.conGuiones = constraintAnnotation.conGuiones();
	    }

	    public boolean isValid(String object, ConstraintValidatorContext constraintContext) {


	    	Pattern nitPattern = Pattern.compile(this.conGuiones == true ? 
	    										"^\\d{4}[-| ]?\\d{6}[-| ]?\\d{3}[-| ]?\\d{1}$" :
	    										"^\\d{14}$");
	    	Matcher nitMatcher = nitPattern.matcher(object);
	    	
	        if (nitMatcher.matches()){
	            return true;
	    	}
	    	return false;
	    }

}
